#include "executor.h"
#include <iostream>
#include <string>
#include <windows.h> // 新增Windows头文件，用于设置控制台编码

// 显示帮助信息
void showHelp() {
    // 设置控制台编码为UTF-8
    SetConsoleOutputCP(CP_UTF8);
    std::cout << "\n===== 智能车控制指令说明 =====" << std::endl;
    std::cout << "  M - 前进（正常1格 / 加速2格）" << std::endl;
    std::cout << "  L - 左转（正常直接转 / 加速先前进1格再转）" << std::endl;
    std::cout << "  R - 右转（正常直接转 / 加速先前进1格再转）" << std::endl;
    std::cout << "  F - 切换加速状态（当前：关闭/开启）" << std::endl;
    std::cout << "  B - 批量执行指令（输入一串指令，如：MRFL）" << std::endl;
    std::cout << "  RST - 重置到初始位置(0,0,N)" << std::endl;
    std::cout << "  H - 显示帮助" << std::endl;
    std::cout << "  Q - 退出程序" << std::endl;
    std::cout << "==============================" << std::endl;
}

// 显示当前车辆状态
void showState(Executor& car) {
    SetConsoleOutputCP(CP_UTF8);
    int32_t x, y;
    char heading;
    bool isAccel;
    car.getCurrentState(x, y, heading, isAccel);
    
    std::cout << "\n【当前状态】" << std::endl;
    std::cout << "  位置：X=" << x << "（EW方向，E+W-） | Y=" << y << "（NS方向，N+S-）" << std::endl;
    std::cout << "  朝向：";
    switch (heading) {
        case 'N': std::cout << "北（North）"; break;
        case 'S': std::cout << "南（South）"; break;
        case 'E': std::cout << "东（East）"; break;
        case 'W': std::cout << "西（West）"; break;
    }
    std::cout << "\n  加速状态：" << (isAccel ? "开启" : "关闭") << std::endl;
    std::cout << "==============================" << std::endl;
}

int main() {
    SetConsoleOutputCP(CP_UTF8);
    Executor car;
    std::string input;
    
    std::cout << "===== 智能车控制系统（客户端）=====" << std::endl;
    showHelp();
    showState(car);
    
    // 交互循环
    while (true) {
        SetConsoleOutputCP(CP_UTF8);
        std::cout << "\n请输入指令（H查看帮助，Q退出）：";
        std::getline(std::cin, input);
        
        // 指令处理
        if (input == "Q" || input == "q") {
            std::cout << "程序已退出！" << std::endl;
            break;
        } else if (input == "H" || input == "h") {
            showHelp();
        } else if (input == "RST" || input == "rst") {
            car.initialize(0, 0, 'N');
            std::cout << "已重置到初始位置(0,0,N)！" << std::endl;
            showState(car);
        } else if (input == "B" || input == "b") {
            std::cout << "请输入批量指令：";
            std::getline(std::cin, input);
            car.executeCommands(input);
            std::cout << "批量指令执行完成！" << std::endl;
            showState(car);
        } else if (input.size() == 1) {
            char cmd = toupper(input[0]);
            if (cmd == 'M' || cmd == 'L' || cmd == 'R' || cmd == 'F') {
                car.executeCommand(cmd);
                showState(car);
            } else {
                std::cout << "无效指令！请输入H查看帮助。" << std::endl;
            }
        } else {
            std::cout << "无效输入！单个指令输入M/L/R/F，或输入B执行批量指令。" << std::endl;
        }
    }
    
    return 0;
}